<style>
    .layui-card:last-child {
        width: 95%;
        /* min-height: 900px; */
        margin: 0 auto;
        margin-top: 32px;
        border-radius: 8px;
        padding: 10px 15px;
        box-sizing: border-box;
    }
    
    .zhuangtai {
        float: left;
        font-size: 18px;
        margin-right: 12px;
    }
    
    .sanbut {
        float: right;
        display: flex;
        justify-content: space-around;
        align-items: center;
        /* position: absolute;
        right: 0;
        top: 0; */
    }
    
    .layui-btn-normal {
        background-color: #FFFFFF;
        color: black;
        border: 1px solid #DCDFE6;
    }
    
    .bai {
        background-color: #FFFFFF;
        color: black;
        border: 1px solid #DCDFE6;
    }
    
    th {
        border: transparent !important;
    }
    
    tr {
        border-bottom: 1px solid #F5F5F5;
    }
    
    .layui-btn-danger {
        background-color: #F87C7E;
    }
    
    .aa {
        border-radius: 8px;
        background-color: #F5F5F5;
        padding-left: 10px;
        color: black;
    }
    
    .bb {
        background-color: #06B991;
        display: inline-block;
        width: 90px;
        height: 38px;
        line-height: 38px;
        padding: 0 18px;
        /* background-color: transparent; */
        color: #fff;
        white-space: nowrap;
        text-align: center;
        font-size: 14px;
        border: none;
        /* border-radius: 2px; */
        cursor: pointer;
        border-radius: 8px;
        /* border: 1px solid #DCDFE6; */
        margin-right: 10px;
    }
    
    .layui-btn {
        display: inline-block;
    }
    
    .cc {
        min-width: 190px;
        font-size: 15px;
    }
    
    .minbox {
        /* width: 300px; */
        height: 200px;
        margin: 14px 0;
        padding: 0 30px;
    }
    
    .layui-layer-page .layui-layer-content {
        width: 360px;
        margin: 0 auto;
    }
    
    .zuobiao {
        width: 100%;
        text-align: center;
        font-size: 22px;
        margin: 23px 0;
    }
    
    .xy {
        font-size: 18px;
        width: 100%;
        text-align: center;
        margin-top: 12px;
    }
    
    .xy>input {
        margin-left: 22.5px;
        border-radius: 8px;
        background-color: #F5F5F5;
        height: 24.5px;
        width: 162px;
        padding-left: 10px;
        border: none;
    }
    
    .put>input {
        margin-left: 5.5px;
        border-radius: 8px;
        background-color: #F5F5F5;
        height: 24.5px;
        width: 162px;
        padding-left: 10px;
        border: none;
    }
    
    .dd {
        background-color: #FEA858;
    }
    
    .tbodyf td,
    .tbodyf th {
        color: red;
    }
    
    .tbodyf>tr>td {
        text-align: left !important;
    }
    
    .layui-form-select .layui-input {
        border-radius: 8px;
        background-color: #F5F5F5;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
            <div class="layui-form-item">
                <!-- 公司名搜索 -->
                <div class="layui-inline">
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                            <select name="interest-search" id="soso" lay-filter="interest-search" lay-search style="background-color: #F5F5F5;">
                        </select>
                        </div>
                    </div>
                </div>
                <!-- 用户名搜索 -->
                <div class="layui-inline">
                    <div class="layui-input-block">
                        <input type="text" name="userName" placeholder="搜索用户名" autocomplete="off" class="layui-input aa">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin bb" lay-submit lay-filter="LAY-user-back-search">搜索</button>
                </div>
                <button class="layui-btn layuiadmin-btn-admin bb" style="float: right;margin-left: 10px;" data-type="add" id="add">新增</button>
            </div>
            <div class="LAY-app-message-btns" style="margin-bottom: 10px;">

            </div>
            <table id="LAY-app-message-all" lay-filter="LAY-app-message-all"></table>
            <script type="text/html" id="table-useradmin-all">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                <!-- <a class="layui-btn layui-btn-normal layui-btn-xs" id="chakan" lay-event="chakan">详情</a> -->
                <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="mima">更改密码</a>
                <a class="layui-btn layui-bg-red layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
        <div id="demo5" style="text-align: center;"></div>
    </div>
</div>
<script src="/start/layui/md5.js"></script>
<script>
    layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table', "laypage"], function() {
        var $ = layui.$,
            admin = layui.admin,
            view = layui.view,
            setter = layui.setter,
            table = layui.table,
            laypage = layui.laypage,
            form = layui.form;


        form.render(null, 'layadmin-userfront-formlist');
        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data) {
            var field = data.field;
            console.log(field)
                //执行重载
            table.reload('LAY-app-message-all', {
                where: field
            });
        });

        admin.req({
            url: setter.request.baseUrl + "company/query",
            data: JSON.stringify({
                pageNum: 1,
                pageSize: 100
            }),
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            done: function(res) {
                console.log(res);
                var html = "<option style='color:#e6e6e6;' value=''>请输入公司名</option>";
                for (i = 0; i < res.info.length; i++) {
                    if (res.info[i].companyName == null) {

                    } else {
                        html += "<option value=" + res.info[i].id + ">" + res.info[i].companyName + "</option>"
                    }
                }
                $("#soso").empty().append(html)
                form.render('select'); //需要渲染一下
            },

        });
        //事件下拉框数据发生变化时
        form.on('select(interest-search)', function(data) {
            // 调用搜索函数
            var data_val = data.value; //当前选中的数据
            console.log(data)
            if (data_val == "") {
                // 查询所有的数据,不传的时候就是查询所有的
                getData(1, '')
            } else {
                getData(1, data_val)
            }
            console.log(data);

        });
        getData(1, '')

        function getData(pageNum, companyId) {
            table.render({
                //接口
                elem: "#LAY-app-message-all",
                url: setter.request.baseUrl + "user/query",
                data: JSON.stringify({
                    a: 111
                }),
                where: {
                    pageNum: pageNum,
                    pageSize: 10,
                    companyId: parseInt(companyId)
                },
                contentType: "application/json;charset=UTF-8",
                method: "POST",
                response: {
                    statusName: "code",
                    statusCode: 0,
                    msgName: "msg",
                    countName: "totalCount",
                    dataName: "info",
                },
                page: false,
                done: function(res, curr, count) {
                    console.log(res)
                    laypage.render({
                        elem: 'demo5',
                        count: res.pageResponseDTO.total,
                        curr: pageNum,
                        limit: 10,
                        jump: function(obj, first) {
                            if (!first) {
                                // that.shuju.pageNum = obj.curr;
                                getData(obj.curr)
                            }
                        }
                    });
                },
                cols: [
                    [
                        // { field: "orderNumber;", title: "订单编号" },
                        //全部
                        {
                            field: "lastUpdateTime",
                            align: 'center',
                            title: "日期"
                        }, {
                            field: "userName",
                            align: 'center',
                            title: "用户名"
                        }, {
                            field: "companyName",
                            title: "公司名称",
                            align: 'center',
                        }, {
                            fixed: "right",
                            title: "操作",
                            align: 'center',
                            toolbar: "#table-useradmin-all"
                        },
                    ],
                ],
            });

        }
        //新增
        var active = {
            add: function() {
                admin.popup({
                    title: '新增公司信息',
                    area: ['500px', '500px'],
                    id: 'LAY-popup-useradmin-add',
                    success: function(layero, index) {
                        view(this.id).render('user/user/form').done(function() {
                            form.render(null, 'layuiadmin-form-admin');
                            //监听提交
                            form.on('submit(LAY-user-back-submit)', function(data) {
                                var field = data.field; //获取提交的字段
                                //新增用户信息  
                                admin.req({
                                    url: setter.request.baseUrl + "user/add",
                                    data: JSON.stringify({
                                        'companyId': field.companyId,
                                        'password': MD5(field.password),
                                        'userName': field.userName
                                    }),
                                    type: "POST",
                                    dataType: "json",
                                    contentType: "application/json;charset=UTF-8",

                                    done: function(res) {

                                        console.log('新增', res);
                                        layui.table.reload('LAY-app-message-all'); //重载表格
                                        layer.close(index); //执行关闭 
                                        layer.msg(res.msg);
                                    },

                                });
                            });
                        });
                    }
                });
            }
        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //修改编辑
        function editworkHandle(parms) {
            console.log(parms)
            admin.req({
                url: setter.request.baseUrl + "user/update",
                data: JSON.stringify(parms),
                type: "POST",
                dataType: "json",
                contentType: "application/json;charset=UTF-8",
                // contentType: "application/json",
                done: function(res) {
                    layui.table.reload("LAY-app-message-all");
                    layer.msg(res.msg);
                },
            });
        }
        //删除模块
        table.on("tool(LAY-app-message-all)", function(obj) {
            var data = obj.data;
            var id = localStorage.getItem('id')
            localStorage.setItem('id', JSON.stringify([]));
            if (obj.event === "del") {
                console.log('11111')
                layer.confirm("真的删除么", function(index) {
                    console.log(data)
                    delcardHandle({
                        idList: data.id,
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === "edit") {
                admin.popup({
                    title: "编辑用户信息",
                    area: ["500px", "500px"],
                    success: function(layero, index) {
                        view(this.id).render("user/user/userform", data)
                            .done(function() {
                                let _data = data;


                                form.render(null, "layuiadmin-form-userform");
                                // var aa = layedit.build('content', {
                                //     height: 500
                                // });
                                // layedit.setContent(aa, _data.content, false)
                                form.on('submit(LAY-user-back-submit)', function(data) {
                                    data.field.companyId = parseInt(data.field.companyId)
                                    var field = data.field;
                                    console.log('111', data)


                                    editworkHandle(Object.assign(_data, field));
                                    layer.close(index);
                                });
                            });
                    },
                });
            } else if (obj.event === "mima") {
                admin.popup({
                    title: "更改密码",
                    area: ["500px", "500px"],
                    success: function(layero, index) {
                        view(this.id).render("user/user/password", data)
                            .done(function() {
                                let _data = data;


                                form.render(null, "layuiadmin-form-mima");
                                // var aa = layedit.build('content', {
                                //     height: 500
                                // });
                                // layedit.setContent(aa, _data.content, false)
                                form.on('submit(LAY-user-back-submit)', function(data) {
                                    var field = data.field;
                                    console.log(field)
                                    if (field.newPassword == field.qrmima) {
                                        admin.req({
                                            url: setter.request.baseUrl + "user/update-password",
                                            data: JSON.stringify({
                                                id: parseInt(field.id),
                                                newPassword: MD5(field.newPassword),
                                                oldPassword: MD5(field.oldPassword)
                                            }),
                                            headers: {
                                                'content-type': 'application/json',
                                            },
                                            type: "post",
                                            done: function(res) {
                                                layer.msg(res.msg);
                                                layer.close(index);
                                                // layer.msg('修改成功')
                                            },
                                        });

                                    } else {
                                        layer.msg('新密码与确认密码不一致...')
                                    }


                                });
                            });
                    },
                });
            }
        });
        //删除
        function delcardHandle(params) {
            console.log(params)
            admin.req({
                url: setter.request.baseUrl + "user/soft-delete",
                data: JSON.stringify({
                    idList: [params.idList]
                }),
                headers: {
                    'content-type': 'application/json',
                },
                type: "post",
                done: function(res) {
                    layer.msg(res.msg);
                },
            });
        }

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

<script type="text/html" id="readstate">
    {{# if(d.readstate ==0){}} {{# return '同意'}} {{# }}} {{# if(d.readstate ==1 ){}} {{# return '拒绝'}} {{# }}}
</script>
<script type="text/html" id="adminreadstate">
    {{# if(d.adminreadstate ==0){}} {{# return '未处理'}} {{# }}} {{# if(d.adminreadstate ==1 ){}} {{# return '已处理'}} {{# }}}
</script>